package com.rsk.service.curriculum.impl;


import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.rsk.dao.curriculum.RskCourseCategoryRepository;
import com.rsk.dao.extend.curriculum.RskCourseCategoryExtend;
import com.rsk.dao.utils.SearchDataUtils;
import com.rsk.entity.curriculum.RskCourseCategory;
import com.rsk.service.curriculum.RskCourseCategoryService;
import com.rsk.utils.algorithm.SnowflakeIdWorker;
import com.rsk.utils.tools.PageUtils;


@Service(interfaceClass = RskCourseCategoryService.class)
public class RskCourseCategoryServiceImpl implements RskCourseCategoryService{
	
	@Autowired
	private RskCourseCategoryRepository rskCourseCategoryRepository;
	
    @Autowired
    private RskCourseCategoryExtend rskCourseCategoryExtend;
    
    @Autowired
    private SearchDataUtils searchDataUtils;

	@Transactional
	public int saveCourseCategory(String categoryName, Long pId, Integer sort, Long operatorUser) {
		RskCourseCategory rskCourseCategory = new RskCourseCategory();
		rskCourseCategory.setId(SnowflakeIdWorker.generateId(true));
		rskCourseCategory.setCategoryName(categoryName);
		rskCourseCategory.setPId(pId);
		rskCourseCategory.setSort(sort);
		rskCourseCategory.setOperatorUser(operatorUser);
		rskCourseCategory.setOperatorTime(new Date());
		rskCourseCategoryRepository.saveAndFlush(rskCourseCategory);
		return 1;
	}
	
	@Transactional
	public int updateCourseCategory(Long id, String categoryName, Integer sort, Long operatorUser) {
		return rskCourseCategoryRepository.updateCourseCategory(id, categoryName, sort, operatorUser);
	}

	@Transactional
	public int delCourseCategory(Long id, Long operatorUser) {
		return rskCourseCategoryRepository.delRskCourseCategory(id, operatorUser);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public PageUtils<Map> selectCourseCategory(String categoryName, Integer nowPage, Integer pageSize) {
		Map<String, Object> map = rskCourseCategoryExtend.selectCourseCategoryByPage(categoryName);
        String sql = map.get("sql").toString();
        String countSQL =  map.get("countSQL").toString();
        Map<String,Object> paramMap = (Map<String, Object>) map.get("paramMap");
        PageUtils<Map> page = searchDataUtils.searchByPage(nowPage, pageSize, sql, countSQL, paramMap, Map.class);
		return page;
	}
	
	@Transactional
	public int updateCourseCategorySee(Long id, Integer isSee, Long operatorUser) {
		return rskCourseCategoryRepository.updateCourseCategorySee(id, isSee, operatorUser);
	}
	
}
